/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package smartgrid;

import org.orm.PersistentException;
public class ContractProcessor {
	private int ID;
	
	private String Number;
	
	private String ClientNif;
	
	private String ClientName;
	
	private String Morada;
	
	private String action="";
	
	public void setID(int value) {
		this.ID = value;
	}
	
	public int getID() {
		return ID;
	}
	
	/**
	 * Número do contrato.
	 */
	public void setNumber(String value) {
		this.Number = value;
	}
	
	public String getNumber() {
		return Number == null ? "" : Number;
	}
	
	/**
	 * Nif do titular do contrato.
	 */
	public void setClientNif(String value) {
		this.ClientNif = value;
	}
	
	public String getClientNif() {
		return ClientNif == null ? "" : ClientNif;
	}
	
	/**
	 * Nome do titular do contrato.
	 */
	public void setClientName(String value) {
		this.ClientName = value;
	}
	
	public String getClientName() {
		return ClientName == null ? "" : ClientName;
	}
	
	public void setMorada(String value) {
		this.Morada = value;
	}
	
	public String getMorada() {
		return Morada == null ? "" : Morada;
	}
	
	public String getAction() {
		return action;
	}
	
	public void setAction(String action) {
		this.action = action;
	}
	
	private int Tariff_CurrentTarif_role_tariffID;
	
	public void setTariff_CurrentTarif_role_tariffID(int value) {
		this.Tariff_CurrentTarif_role_tariffID = value;
	}
	
	public int getTariff_CurrentTarif_role_tariffID() {
		return Tariff_CurrentTarif_role_tariffID;
	}
	
	private int supplier_role_supplierID;
	
	public void setSupplier_role_supplierID(int value) {
		this.supplier_role_supplierID = value;
	}
	
	public int getSupplier_role_supplierID() {
		return supplier_role_supplierID;
	}
	
	public String process() {
		String result = "Unexcepted result";
		if (action.equals("search")) {
			smartgrid.Contract _contract = smartgrid.Contract.loadContractByORMID(getID());
			if (_contract!= null) {
				copyFromBean(_contract);
				result = "Search success";
			}
			else {
				result = "Search failed";
			}
		}
		else if(action.equals("insert"))  {
			try {
				smartgrid.Contract _contract = smartgrid.Contract.createContract();
				copyToBean(_contract);
				if (_contract.save()) {
					result = "Insert success";
				}
				else {
					result = "Insert failed";
				}
			}
			catch (Exception e) {
				result = "Insert error: " + e.toString();
			}
		}
		else if (action.equals("update")) {
			smartgrid.Contract _contract= smartgrid.Contract.loadContractByORMID(getID());
			if (_contract != null) {
				copyToBean(_contract);
				if (_contract.save()) {
					result = "Update success";
				}
				 else {
					result = "Update failed";
				}
			}
			 else  {
				result = "Update failed";
			}
			
		}
		else if (action.equals("delete")) {
			smartgrid.Contract _contract = smartgrid.Contract.loadContractByORMID(getID());
			if (_contract != null && _contract.deleteAndDissociate()) {
				result = "Delete success";
			}
			else {
				result = "Delete failed";
			}
		}
		else if (action.equals("")) {
			result = "";
		}
		action = "";
		return result;
	}
	
	private void copyFromBean(smartgrid.Contract _contract) {
		setNumber(_contract.getNumber());
		setClientNif(_contract.getClientNif());
		setClientName(_contract.getClientName());
		setMorada(_contract.getMorada());
		setID(_contract.getORMID());
		
		{
			smartgrid.Tariff _tariff = _contract.getTariff_CurrentTarif_role();
			if (_tariff != null) {
				setTariff_CurrentTarif_role_tariffID(_tariff.getORMID());
			}
		}
		
		
		{
			smartgrid.Supplier _supplier = _contract.getSupplier_role();
			if (_supplier != null) {
				setSupplier_role_supplierID(_supplier.getORMID());
			}
		}
		
	}
	
	private void copyToBean(smartgrid.Contract _contract) {
		_contract.setNumber(getNumber());
		_contract.setClientNif(getClientNif());
		_contract.setClientName(getClientName());
		_contract.setMorada(getMorada());
		smartgrid.Tariff _tariff_CurrentTarif_role = smartgrid.Tariff.loadTariffByORMID(getTariff_CurrentTarif_role_tariffID());
		_contract.setTariff_CurrentTarif_role(_tariff_CurrentTarif_role);
		smartgrid.Supplier _supplier_role = smartgrid.Supplier.loadSupplierByORMID(getSupplier_role_supplierID());
		_contract.setSupplier_role(_supplier_role);
	}
	
}

